﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Input;
using ExCombatAssist.Settings;
using ff14bot;
using ff14bot.Helpers;
using ff14bot.Managers;

namespace ExCombatAssist
{
    public partial class ExCombatAssistSettings : Form
    {
        public ExCombatAssistSettings()
        {
            InitializeComponent();
        }

        #region CBOItem from nomnomnom
        public class CboItem
        {
            public readonly int E;
            private readonly string _s;

            public CboItem(int pe, string ps)
            {
                E = pe;
                _s = ps;
            }

            public override string ToString()
            {
                return _s;
            }
        }
        #endregion
        #region Combobox ENUM - Required for ENUM in comboboxes from nomonom
        private static void SetComboBoxEnum(ComboBox cb, int e)
        {
            CboItem item;
            for (var i = 0; i < cb.Items.Count; i++)
            {
                item = (CboItem)cb.Items[i];
                if (item.E != e) continue;
                cb.SelectedIndex = i;
                return;
            }
            item = (CboItem)cb.Items[0];
            Logging.Write("[Ex Combat Assist] Dialog Error: Combobox {0} does not have enum({1}) in list, defaulting to enum({2})",
                          cb.Name, e, item.E);
            cb.SelectedIndex = 0;
        }
        private static int GetComboBoxEnum(ComboBox cb)
        {
            var item = (CboItem)cb.Items[cb.SelectedIndex];
            return item.E;
        }
        #endregion

        private void ExCombatAssistSettings_Load(object sender, EventArgs e)
        {

            #region GUI code from nomnomnom
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.Alt, "Alt - Mod"));
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.Control, "Ctrl - Mod"));
            ModifierKeyComboBox.Items.Add(new CboItem((int)System.Windows.Input.ModifierKeys.Shift, "Shift - Mod"));
            ModifierKeyComboBox.Items.Add(new CboItem((int)ModifierKeys, "Disable HK"));
            SetComboBoxEnum(ModifierKeyComboBox, (int)ExCombatAssistSets.instance.ModKeyChoice);

            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.None, "Modifier Only"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.XButton1, "Mouse button 4"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.XButton2, "Mouse button 5"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.D1, "1 (no numpad)"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.D2, "2 (no numpad)"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.D3, "3 (no numpad)"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.Q, "Q"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.E, "E"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.R, "R"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.Z, "Z"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.X, "X"));
            PauseKeyComboBox.Items.Add(new CboItem((int)Keys.C, "C"));
            SetComboBoxEnum(PauseKeyComboBox, (int)ExCombatAssistSets.instance.PauseKeyChoice);
            #endregion

            lstDoNothing.DataSource = ExCombatAssistSets.instance.DontDoActionList;
            chkFrameLock.Checked = ExCombatAssistSets.instance.UseFrameLock;
        }

        private void ModifierKeyComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExCombatAssistSets.instance.ModKeyChoice = (ModifierKeys)GetComboBoxEnum(ModifierKeyComboBox);
            RemoveHotkeys();
            RegisterHotkeys();
        }

        private void PauseKeyComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExCombatAssistSets.instance.PauseKeyChoice = (Keys)GetComboBoxEnum(PauseKeyComboBox);
            RemoveHotkeys();
            RegisterHotkeys();
        }

        public void RemoveHotkeys()
        {
            HotkeyManager.Unregister("Pause Key");
        }

        public void RegisterHotkeys()
        {
            HotkeyManager.Register("Pause Key", ExCombatAssistSets.instance.PauseKeyChoice, ExCombatAssistSets.instance.ModKeyChoice, hk => { ExCombatAssistSets.instance.IsPaused = !ExCombatAssistSets.instance.IsPaused; Logging.Write(ExCombatAssistSets.instance.IsPaused ? "ExCombatAssist Paused!" : "ExCombatAssist Resumed!"); });
        }

        private void chkFrameLock_CheckedChanged(object sender, EventArgs e)
        {
            ExCombatAssistSets.instance.UseFrameLock = chkFrameLock.Checked;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            ExCombatAssistSets.instance.Save();
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ExCombatAssistSets.instance.DontDoActionList.Add(txtAddToList.Text);
            txtAddToList.Clear();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            ExCombatAssistSets.instance.DontDoActionList.Remove((string) lstDoNothing.SelectedValue);
        }
  }
}
